﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
//using Microsoft.PowerShell.Commands;

namespace BELT
{
    [Cmdlet("New", "IEBrowser")]
    public class NewIEBrowserCommand : Cmdlet
    {
        public static Object IEHandle = null;
        private string _starturl = "about:blank";
        //private string _starturl = "google.com";
        [Parameter(ValueFromPipeline = false)]
        public SwitchParameter InVisible { set; get; }
        
        [Parameter(Mandatory=false,ValueFromPipeline=false,Position=0)]
        public string StartURL {
            get {
                return _starturl;
            }
            set {
                _starturl = value;
            }
        }
        [Parameter(ValueFromPipeline = false)]
        public SwitchParameter WaitIE { set; get; }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            Type ieType = Type.GetTypeFromProgID("InternetExplorer.Application");
            Object ieObject = Activator.CreateInstance(ieType);
            IEHandle = ieObject;
            if (InVisible.IsPresent)
            {
                ieType.InvokeMember("Visible", BindingFlags.SetProperty, null, ieObject, new object[1] { false });
            }
            else
            {
                ieType.InvokeMember("Visible", BindingFlags.SetProperty, null, ieObject, new object[1] { true });
            }
            ieType.InvokeMember("Navigate", BindingFlags.InvokeMethod, null, ieObject, new object[1] { _starturl });
            WriteObject(ieObject);
            if (WaitIE.IsPresent)
            {
                WaitIEBrowserCommand.Wait();
            }
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }
}
